define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('预约人'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'out_trade_no', title: __('Out_trade_no'), operate: 'LIKE'},
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'a_price', title: __('A_price'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: 'LIKE'},
                        //{field: 'travelers_id', title: __('Travelers_id'), operate: 'LIKE'},
                        {field: 'travelers_count', title: __('出行人数')},
                        {field: 'fx_pirce', title: __('分销价')},
                        {field: 'zt_rate', title: __('直推分佣百分比')},
                        {field: 'jt_rate', title: __('间推分佣百分比')},
                        {field: 'user2.username', title: __('直推代理')},
                        {field: 'user3.username', title: __('间推代理')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'is_pay', title: __('是否支付'), searchList: {"0":__('未支付'),"1":__('已支付')}, formatter: Table.api.formatter.status},
                        {field: 'pay_time', title: __('支付时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'list',
                                    text: '核销订单',
                                    title: '核销订单',
                                    classname: 'btn btn-xs btn-warning  btn-ajax',
                                    icon: 'fa fa-list',
                                    url: 'order/writeOff',
                                    visible: function (row) {
                                        // 判断当前行数据中的 status 字段的值是否等于 1
                                        if (row.status == '0') {
                                            // return true 代表显示
                                            return true;
                                        }
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click");
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data,rest) {
                                        var opt = {
                                            url: '',
                                        };
                                        //刷新table表单
                                        table.bootstrapTable('refresh', opt);
                                        return true;
                                    }
                                },
                                {
                                    name: 'list',
                                    text: '取消订单',
                                    title: '取消订单',
                                    classname: 'btn btn-xs btn-primary  btn-ajax',
                                    icon: 'fa fa-list',
                                    url: 'order/cancel',
                                    visible: function (row) {
                                        // 判断当前行数据中的 status 字段的值是否等于 1
                                        if (row.status == '0') {
                                            // return true 代表显示
                                            return true;
                                        }
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click");
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data,rest) {
                                        var opt = {
                                            url: '',
                                        };
                                        //刷新table表单
                                        table.bootstrapTable('refresh', opt);
                                        return true;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
